package club.dataresources.controller;

import club.dataresources.bo.MenuBo;
import club.dataresources.bo.PersonalMenuBo;
import club.dataresources.bo.ProductBo;
import club.dataresources.bo.ServiceBo;
import club.dataresources.common.R;
import club.dataresources.entity.*;
import club.dataresources.mapper.ProductMenuDetailMapper;
import club.dataresources.mapper.ServiceMenuDetailMapper;
import club.dataresources.service.MenuService;
import club.dataresources.service.ProductService;
import club.dataresources.service.ServiceService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户选择套餐 服务
 *
 * @author
 * @create 2018-11-12 10:57
 **/
@RequestMapping(value = "/menu")
@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ServiceService serviceService;

    @Autowired
    private ProductMenuDetailMapper productMenuDetailMapper;

    @Autowired
    private ServiceMenuDetailMapper serviceMenuDetailMapper;

    /**
     * 展示套餐
     */
    @RequestMapping("/listMenu")
    public @ResponseBody
    R listMenu(@RequestBody MenuBo menuBo){
        System.out.println("========前台========查询显示套餐================");
        try{
            Page<Menu> liPage = menuService.listMenu(menuBo.getPage(),menuBo.getRows(),menuBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/getMenuById")
    public @ResponseBody
    R getMenuById(@RequestParam("menuId") Integer menuId){
        System.out.println("========前台========根据menuId查询显示理发产品和理发类型 套餐内容================");
        try{
            Map<String,Object> map = new HashMap<String, Object>();
            map = menuService.detailMenuById(menuId);
            map.put("menuId",menuId);
            return R.ok().put("data",map).put("code",200).put("msg","显示完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listProduct")
    public @ResponseBody
    R listProduct(HttpServletRequest request,
                  HttpServletResponse response,
                  @RequestBody ProductBo productBo){
        System.out.println("========前台========查询理发产品的结果================");
        try {
            Page<Product> liPage = productService.listProduct(productBo.getPage(),productBo.getRows(),productBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listService")
    public @ResponseBody
    R listService(HttpServletRequest request,
                  HttpServletResponse response,
                  @RequestBody ServiceBo serviceBo){
        System.out.println("========前台========查询理发类型的结果================");
        try {
            Page<Service> list = serviceService.listService(serviceBo.getPage(),serviceBo.getRows(),serviceBo);
            return R.ok().put("data",list.getResult()).put("code",200).put("msg","显示完毕").put("total",list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    /**
     * 添加到自定义套餐
     * 用户决定menu_name\menu_description\menu_img
     * 系统决定menu_id\menu_price\menu_count\menu_by
     */
    @RequestMapping("/addPersonalMenu")
    public @ResponseBody
    R addPersonalMenu(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody PersonalMenuBo personalMenuBo){
        System.out.println("========前台========用户添加自定义套餐================");
        try{
            //计算menu_count和menu_price  menu_count默认为0.9打九折
            List<Integer> liProduct = personalMenuBo.getLiProduct();
            float sumProduct = 0;
            for (Integer p:liProduct) {
                sumProduct = sumProduct + productService.findProByPid(p).getProductCost();
            }

            List<Integer> liService = personalMenuBo.getLiService();
            float sumService = 0;
            for(Integer s:liService){
                sumService = sumService + serviceService.findSerBySerId(s).getServiceCost();
            }

            float menu_count = (float)0.9;
            personalMenuBo.setMenuCount(menu_count);

            float menu_price = (sumProduct + sumService)*menu_count;
            personalMenuBo.setMenuPrice(menu_count);

            Integer id = (Integer)request.getSession().getAttribute("customerId");
            if(id!=null && !id.equals(" ")){
                personalMenuBo.setMenuBy(id);
            }else{
                return R.error().put("code",500).put("msg","拿到的customerId不合法");
            }

            //新增套餐
            menuService.addAndReturnId(personalMenuBo);

            //拿到了自定义的菜单menu_id
            Integer menuId = personalMenuBo.getMenuId();
            if(menuId!=null){
                for(Integer p:liProduct){
                    ProductMenuDetail pmd = new ProductMenuDetail();
                    pmd.setMenuId(menuId);
                    pmd.setProductId(p);
                    productMenuDetailMapper.insertSelective(pmd);
                }
                for(Integer s:liService){
                    ServiceMenuDetail smd = new ServiceMenuDetail();
                    smd.setMenuId(menuId);
                    smd.setServiceId(s);
                    serviceMenuDetailMapper.insertSelective(smd);
                }
                return R.ok().put("code",200).put("data",personalMenuBo).put("msg","添加成功");
            }else{
                return R.error().put("code",500).put("msg","添加菜单后拿到的menuId主键为空值");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
